# Alexander F. Gazmararian
# afg2@princeton.edu

library(tidyverse)
library(here)
library(modelsummary)

nat <- readRDS(here("data", "national_wide.rds"))

min(as.Date(nat$EndDate))
max(as.Date(nat$EndDate))

summary(nat$duration) /60

glimpse(nat)
`NA` <- function(x) {sum(is.na(x))}

file_out <- here("output", "tables", "si_tab_4.1.txt")

nat %>%
  mutate(
    q1 = as.integer(income5 == "Less than $29,999"),
    q2 = as.integer(income5 == "$30,000 - $59,999"),
    q3 = as.integer(income5 == "$60,000 - $99,999"),
    q4 = as.integer(income5 == "$100,000 or more"),
    ns = as.integer(income5 == "Prefer not to say")
  ) %>%
  datasummary(
    formula = (Age = age) + (Female = female) + (Black = black) + (Hispanic = hispanic) +
      (Employed = fullemploy) +
      (`Income Q1` = q1) + (`Income Q2` = q2) + (`Income Q3` = q3) + (`Income Q4` = q4) + (`Income Not Say` = ns) +
      (College = college) +
      (Democrat = dem) + (Republican = rep) + 
      (`Climate Concern` = gw_concern_bin) ~ Mean + SD + N + `NA`,
    output = "latex",
    escape = FALSE
  ) %>%
  cat(., file = file_out)
tbl_fix <- readLines(file_out, warn = FALSE)
tbl_fix <- tbl_fix[-c(1,2,23)]
cat(tbl_fix, file = file_out)
